// pages/express/publish/publish.js
Page({
  data: {
    formData: {
      company: '',
      pickupCode: '',
      pickupLocation: '',
      deliveryLocation: '',
      detailAddress: '',
      phone: '',
      requiredTime: '',
      reward: '',
      urgent: false,
      description: ''
    },
    expressCompanies: ['中通快递', '圆通速递', '申通快递', '韵达快递', '顺丰速运', '京东快递', '邮政EMS'],
    pickupLocations: ['菜鸟驿站(东门)', '菜鸟驿站(南门)', '快递柜A区', '快递柜B区', '快递柜C区'],
    deliveryLocations: ['宿舍楼1号', '宿舍楼2号', '宿舍楼3号', '宿舍楼4号', '教学楼', '图书馆'],
    companyIndex: -1,
    pickupIndex: -1,
    deliveryIndex: -1,
    timeRange: [
      ['今天', '明天', '后天'],
      ['上午', '下午', '晚上'],
      ['不限', '12:00前', '18:00前', '20:00前']
    ],
    timeIndex: [0, 0, 0],
    timeText: ''
  },

  onLoad() {
    // 初始化
  },

  onInputChange(e) {
    const { field } = e.currentTarget.dataset
    const { value } = e.detail
    
    this.setData({
      [`formData.${field}`]: value
    })
  },

  onCompanyChange(e) {
    const index = e.detail.value
    this.setData({
      companyIndex: index,
      'formData.company': this.data.expressCompanies[index]
    })
  },

  onPickupLocationChange(e) {
    const index = e.detail.value
    this.setData({
      pickupIndex: index,
      'formData.pickupLocation': this.data.pickupLocations[index]
    })
  },

  onDeliveryLocationChange(e) {
    const index = e.detail.value
    this.setData({
      deliveryIndex: index,
      'formData.deliveryLocation': this.data.deliveryLocations[index]
    })
  },

  onTimeChange(e) {
    const timeIndex = e.detail.value
    const timeRange = this.data.timeRange
    const timeText = `${timeRange[0][timeIndex[0]]} ${timeRange[1][timeIndex[1]]} ${timeRange[2][timeIndex[2]]}`
    
    this.setData({
      timeIndex,
      timeText,
      'formData.requiredTime': timeText
    })
  },

  onUrgentChange(e) {
    this.setData({
      'formData.urgent': e.detail.value
    })
  },

  validateForm() {
    const { formData } = this.data
    
    if (!formData.company) {
      wx.showToast({ title: '请选择快递公司', icon: 'none' })
      return false
    }
    
    if (!formData.pickupCode.trim()) {
      wx.showToast({ title: '请输入取件码', icon: 'none' })
      return false
    }
    
    if (!formData.pickupLocation) {
      wx.showToast({ title: '请选择取件地点', icon: 'none' })
      return false
    }
    
    if (!formData.deliveryLocation) {
      wx.showToast({ title: '请选择收货地址', icon: 'none' })
      return false
    }
    
    if (!formData.phone.trim()) {
      wx.showToast({ title: '请输入联系电话', icon: 'none' })
      return false
    }
    
    if (!/^1[3-9]\d{9}$/.test(formData.phone)) {
      wx.showToast({ title: '请输入正确的手机号', icon: 'none' })
      return false
    }
    
    if (!formData.requiredTime) {
      wx.showToast({ title: '请选择送达时间', icon: 'none' })
      return false
    }
    
    if (!formData.reward || formData.reward <= 0) {
      wx.showToast({ title: '请输入有效的代拿费用', icon: 'none' })
      return false
    }
    
    return true
  },

  onPreview() {
    if (!this.validateForm()) {
      return
    }

    const { formData } = this.data
    const previewText = `快递公司：${formData.company}
取件码：${formData.pickupCode}
取件地点：${formData.pickupLocation}
收货地址：${formData.deliveryLocation}${formData.detailAddress ? ' ' + formData.detailAddress : ''}
联系电话：${formData.phone}
送达时间：${formData.requiredTime}
代拿费用：¥${formData.reward}
${formData.description ? '特殊要求：' + formData.description : ''}
${formData.urgent ? '【紧急需求】' : ''}`

    wx.showModal({
      title: '预览发布内容',
      content: previewText,
      confirmText: '确认发布',
      success: (res) => {
        if (res.confirm) {
          this.onSubmit()
        }
      }
    })
  },

  onSubmit() {
    if (!this.validateForm()) {
      return
    }

    wx.showLoading({ title: '发布中...' })
    
    // 模拟API调用
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            wx.navigateBack()
          }, 2000)
        }
      })
    }, 1500)
  }
})